package com.noname.lib_base_java.util;

import android.app.Activity;

import java.util.HashSet;

/**
 * Author: Lmoumou
 * Date: 2018-12-05 16:45
 * 文件名: ActivityManager
 * 描述:Activity管理
 */
public class ActivityManager {

    private static HashSet<Activity> hashSet = new HashSet<Activity>();

    private static ActivityManager instance = null;

    private ActivityManager() {
    }

    /**
     * 单例
     *
     * @return
     */
    public static ActivityManager getInstance() {
        synchronized (ActivityManager.class) {
            if (instance == null) {
                instance = new ActivityManager();
            }
        }
        return instance;
    }


    /**
     * 添加Activity元素
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
        try {
            hashSet.add(activity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 移除指定Activity
     *
     * @param activity
     */
    public void removeActivity(Activity activity) {
        try {
            hashSet.remove(activity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 结束所有界面
     */
    public void exit() {
        try {
            for (Activity activity : hashSet) {
                activity.finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
